﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Tron.Players.Simple
{
    class HumanPlayer : Player
    {
        Vector2 direction;
        public bool WaitingForKeyDown {get; private set;}
        public bool WaitingForKeyUp {get; private set;}

        public HumanPlayer(int playerIndex)
            : base(playerIndex)
        {
            WaitingForKeyDown = true;
            WaitingForKeyUp = false;
        }

        public override void Update(Tron.DataStructures.Percepts percepts, Tron.DataStructures.Actions actions)
        {
            if(direction != Vector2.Zero)
                actions.MoveDirection = direction;
            WaitingForKeyUp = true;
        }

        public void UpdateInput()
        {
            WaitingForKeyDown = false;
            KeyboardState keyboardState = Keyboard.GetState();
            if (keyboardState.IsKeyDown(Keys.Up))
            {
                direction = -Vector2.UnitY;
            }
            else if (keyboardState.IsKeyDown(Keys.Down))
            {
                direction = Vector2.UnitY;
            }
            else if (keyboardState.IsKeyDown(Keys.Left))
            {
                direction = -Vector2.UnitX;
            }
            else if (keyboardState.IsKeyDown(Keys.Right))
            {
                direction = Vector2.UnitX;
            }
            else
            {
                WaitingForKeyDown = true;
                WaitingForKeyUp = false;
            }
        }
    }
}
